import { GCellRenderer } from "../../../../com/mui/controls/GCellRenderer";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { EventName } from "../../mvc/EventName";
import { ObjCreate } from "../common/ObjCreate";
import { ImagesConst } from "../../resource/ImagesConst";
import { GTextFormat } from "../../../common/GTextFormat";
import { ColorConfig } from "../../resource/ColorConfig";
import { GlobalStyle } from "../../../common/global/GlobalStyle";
import { BaseItem } from "../common/BaseItem";
import { Language } from "../../../../extend/language/Language";
import { TActivePrevilege } from "../../../../Message/Db/Tables/TActivePrevilege";
import { ItemData } from "../../resource/info/ItemData";
import { GiftBagConfig } from "../../resource/GiftBagConfig";
import { Cache } from "../../cache/Cache";
import { FilterConst } from "../../../component/gconst/FilterConst";
import { ToolTipInfo } from "../../model/ToolTipInfo";
import { NormalTooltip } from "../common/tooltip/NormalTooltip";
import { ItemsUtil } from "../shortcuts/item/ItemsUtil";
import { GameConst } from "../../../component/gconst/GameConst";
import { BuyAlert } from "../pet/BuyAlert";
import { EPrictUnit } from "../../../../Message/Public/EPrictUnit";
import { Alert } from "../../../../com/mui/controls/Alert";
import { DataEvent } from "../../events/DataEvent";
import { FlyToNavbarTool } from "../award/FlyToNavbarTool";
import { ScaleBitmap } from "../../../../com/mui/display/ScaleBitmap";
import { GTextFiled } from "../../../../com/mui/controls/GTextFiled";
import { GButton } from "../../../../com/mui/controls/GButton";
type int = number;
//class ActivityPrivilegeRenderder
    
    export  class ActivityPrivilegeRenderder extends GCellRenderer
    {
        constructor()
        {
            super();
            Dispatcher.addEventListener(EventName.ActivityPrivilegeBuySuccess, this.onActivityPrivilegeBuySuccessHandler);
            return;
        }

        protected /* override */ configUI(): void
        {
            super.configUI();
            this._bg = ObjCreate.createBg(2, 2, 117, 160, this, ImagesConst.RechargeBottom);
            var loc1=new GTextFormat("", 12, 16776960, null, null, null, null, null, flash.text.TextFormatAlign.CENTER);
            var loc2=new GTextFormat("", 12, ColorConfig.instance.getItemColor(4).intColor, true, null, null, null, null, flash.text.TextFormatAlign.CENTER);
            ObjCreate.createBg(0, 5, 117, 20, this, ImagesConst.FightingStrgBg);
            this._nameTxt = ObjCreate.createGTextField("", 0, 5, 117, 20, this, loc2);
            this._ypriceTxt = ObjCreate.createGTextField("", 15, 95, 117, 20, this, GlobalStyle.textFormatRed);
            this._ypriceTxt.autoSize = flash.text.TextFieldAutoSize.LEFT;
            this._line = new Object /* flash.display.Shape */();
            this.addChild(this._line);
            this._line.graphics.lineStyle(1, 16711680, 0.75);
            this._line.graphics.moveTo(10, 102);
            this._line.graphics.lineTo(105, 102);
            this._line.graphics.endFill();
            this._xpriceTxt = ObjCreate.createGTextField("", 15, 110, 117, 20, this, GlobalStyle.textFormat6);
            this._xpriceTxt.autoSize = flash.text.TextFieldAutoSize.LEFT;
            ObjCreate.createBg(27, 30, 64, 64, this, ImagesConst.PackItemBg);
            this._baseItem = new BaseItem();
            this._baseItem.move(29, 32);
            this._baseItem.setSize(60, 60);
            this._baseItem.isDragAble = false;
            this._baseItem.isDropAble = false;
            addChild(this._baseItem);
            this._buyBtn = ObjCreate.createGButton(Language.getString(20437), 33, 133, 50, 25, this, "ButtonRed");
            this._buyBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onBuyBtnClickHandler);
            return;
        }

        public /* override */ set data(arg1: Object)
        {
            super.data = arg1;
            if (arg1 && arg1 is TActivePrevilege) 
            {
                this._apInfo = arg1 as TActivePrevilege;
                this._giftItemData = new ItemData(this._apInfo.giftCode);
                this._rewardItemData = new ItemData(this._apInfo.awardCode);
                this._giftName = "<font color=\'" + this._giftItemData.itemInfo.colorStr + "\'>" + this._apInfo.name + "</font>";
                this._nameTxt.htmlText = this._giftName;
                this._ypriceTxt.text = Language.getStringByParam(58039, this._apInfo.value);
                this._xpriceTxt.text = Language.getStringByParam(58040, this._apInfo.costGold);
                this._baseItem.itemData = this._giftItemData;
                this._giftBagInfoArray = GiftBagConfig.instance.getInfoByType(this._apInfo.giftCode);
                this._baseItem.toolTipDataFunction = this.getToolTipData;
                this._isBuyed = Cache.instance.activityPrivilege.isBuyed(this._apInfo.type);
                this._buyBtn.enabled = !this._isBuyed;
                this._buyBtn.drawNow();
                if (this._isBuyed) 
                {
                    this._buyedApInfo = Cache.instance.activityPrivilege.getBuyedApInfo(this._apInfo.type);
                    this._ypriceTxt.text = Language.getStringByParam(58039, this._buyedApInfo.value);
                    this._xpriceTxt.text = Language.getStringByParam(58040, this._buyedApInfo.costGold);
                }
            }
            return;
        }

        public /* override */ set selected(arg1: Boolean)
        {
            super.selected = arg1;
            if (arg1) 
            {
                this._bg.filters = [FilterConst.gradientGlowFilter];
            }
            else 
            {
                this._bg.filters = [];
            }
            return;
        }

        /* internal  */getToolTipData(arg1: Object): ToolTipInfo
        {
            var loc1=[];
            var loc2="";
            loc1.push(this._giftName);
            if (!this._toolTipInfo) 
            {
                this._toolTipInfo = new ToolTipInfo(NormalTooltip.ToolTipRenderType_Separate, loc1);
            }
            loc2 = Language.getString(58037);
            loc2 = loc2 + ("    " + ItemsUtil.getItemName(this._giftItemData) + "\n");
            loc2 = loc2 + ("    " + Language.getString(58042));
            loc2 = loc2 + ("    " + this.getItemListString());
            loc2 = loc2 + Language.getStringByParam(58038, GameConst.ActivePrevilegeAwardDays);
            loc2 = loc2 + ("    " + ItemsUtil.getItemName(this._rewardItemData) + "*" + this._apInfo.awardAmount);
            loc1.push(loc2);
            this._toolTipInfo.tooltipData = loc1;
            return this._toolTipInfo;
        }

        /* internal  */getItemListString(): string
        {
            var loc2=null;
            var loc3=0;
            var loc1="";
            if (this._giftBagInfoArray) 
            {
                loc3 = 0;
                while (loc3 < this._giftBagInfoArray.length) 
                {
                    loc2 = this._giftBagInfoArray[loc3];
                    if (loc2.item) 
                    {
                        loc1 = loc1 + (ItemsUtil.getItemName(loc2.item) + "*" + loc2.num);
                        if (loc3 != (this._giftBagInfoArray.length - 1)) 
                        {
                            loc1 = loc1 + "\n";
                        }
                    }
                    ++loc3;
                }
            }
            return "<textFormat leading=\'5\'>" + loc1 + "</textFormat>";
        }

        /* internal  */onBuyBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var e: Object /* flash.events.MouseEvent */;
            var isAbundant: Boolean;
            var htmlContent: string;
            var onClickHandler: Function;

            var loc1;
            isAbundant = false;
            htmlContent = null;
            e = arg1;
            if (this._apInfo) 
            {
                isAbundant = BuyAlert.checkMoney(EPrictUnit._EPriceUnitGold, this._apInfo.costGold);
                if (isAbundant) 
                {
                    onClickHandler = (arg1: int): void
                    {
                        if (arg1 == Alert.OK) 
                        {
                            Dispatcher.dispatchEvent(new DataEvent(EventName.ActivityPrivilegeBuy, _apInfo.id));
                            Alert.resetToDefault();
                        }
                        return;
                    }
                    htmlContent = Language.getStringByParam(54172, 1, this._giftName, this._apInfo.costGold, Language.getString(54328));
                    Alert.show(htmlContent, null, Alert.OK | Alert.CANCEL, null, onClickHandler);
                }
            }
            return;
        }

        /* internal  */onActivityPrivilegeBuySuccessHandler(arg1: DataEvent): void
        {
            var loc2=null;
            var loc1=arg1.data as int;
            if (this._apInfo && this._apInfo.id == loc1) 
            {
                loc2 = this._baseItem.localToGlobal(new Object /* flash.geom.Point */(3, 3));
                FlyToNavbarTool.flyToBackPack(this._baseItem.bitmapdata, loc2);
            }
            return;
        }

        /* internal */ /* var */_bg: ScaleBitmap;

        /* internal */ /* var */_nameTxt: GTextFiled;

        /* internal */ /* var */_ypriceTxt: GTextFiled;

        /* internal */ /* var */_xpriceTxt: GTextFiled;

        /* internal */ /* var */_line: Object /* flash.display.Shape */;

        /* internal */ /* var */_baseItem: BaseItem;

        /* internal */ /* var */_buyBtn: GButton;

        /* internal */ /* var */_apInfo: TActivePrevilege;

        /* internal */ /* var */_giftItemData: ItemData;

        /* internal */ /* var */_giftBagInfoArray: Array<any>;

        /* internal */ /* var */_rewardItemData: ItemData;

        /* internal */ /* var */_giftName: string="";

        /* internal */ /* var */_isBuyed: Boolean;

        /* internal */ /* var */_buyedApInfo: TActivePrevilege;

        /* internal */ /* var */_toolTipInfo: ToolTipInfo;
    }
